*****Conscription and Military Service: Do They Result in Future Violent and Non-Violent Incarcerations and Recidivism?  Wang, Flores-Lagunes 2020*****
program define I2_pop_test_be, eclass

marksample touse

set more off


          foreach birthgroup in 5  { 
  
                          foreach race in $race4852 {
   local outcome="$outcome1"  						  					  
				 		
   preserve					
						
   di "bounds for `outcome' for males born in `birthgroup' white=`race'"						
   
   gen cohort_sample=. 

   *Note that the cohort_sample also mark out the VSUS records 
   replace cohort_sample=1 if ((white==`race' & inmate==0 & ygroup`birthgroup'==1 ) /// 
   |( white==`race' & totalSample==1 & ygroup`birthgroup'==1 & `outcome'!=9 & inmate==1 & Census==0))

   replace cohort_sample=0 if cohort_sample==. & (Census==0|inmate==0) 
   
   ****Step 3: Create birth_cohort variable to mark out the corresponding birth cohort
   gen birth_cohort=.
   replace birth_cohort=1 if white==`race' & ygroup`birthgroup'==1 & (inmate==0 | Census==0) 
   replace birth_cohort=0 if birth_cohort==. & (inmate==0 | Census==0) 
   

   ***Step 5: svy_male_inmate is the variable with the values of the total inmates incarcerated in state prison-
   * -in 1979, 1986, and 1991, and in the federal prison in 1991 respectively 
   gen svy_male_inmate=total_male_inmate if Census==0 

   gen T=vv
   gen Z=eligible
   ***Step 6: Y is the binary indicator to compute the proportion of inmates conditional on veteran status and draft-eligibilities-
   * with I2_pop_survey_be
   gen Y=`outcome'*cohort_sample
   replace Y=0 if Y==. & (Census==0|inmate==0)
   gen W=weight 
   
   ***Population estimates 
   **Strata proportion 
   
   if `race'==1 {
   global race 1
   
   di "input the proportion of the white sample"
   sca Cc=0.1556
   sca li Cc 
   
   sca Cat=0.1261
   sca li Cat 
   
   sca Cnt=0.7183
   sca li Cnt 
   
   **Probabilities 
   sca p10=Cat
   sca p01=Cnt
   sca p11=Cat+Cc
   sca p00=Cnt+Cc
   sca li p10
   sca li p01
   sca li p11 
   sca li p00 
   
   **Conditional means of veterans and eligible 
   sca PrT1=0.1692
   sca li PrT1
   sca PrT0=1-0.1692
   sca li PrT0
   
   sca PrZ1=0.2770
   sca li PrZ1 
   sca PrZ0=1-0.2770
   sca li PrZ0
   
   
   }
   else  {   
   
   global race 0
   
   di "Input the proportion of the nonwhite sample"
   sca Cc=0.0353
   sca li Cc 
   
   sca Cat=0.1266 
   sca li Cat 
   
   sca Cnt=0.8381
   sca li Cnt 
   
   **Probabilities 
   sca p10=Cat
   sca p01=Cnt
   sca p11=Cat+Cc
   sca p00=Cnt+Cc
   sca li p10
   sca li p01
   sca li p11 
   sca li p00 
   
   **Conditional means of veterans and eligible 
   sca PrT1=0.1366
   sca li PrT1
   sca PrT0=1-0.1366
   sca li PrT0
   
   sca PrZ1=0.2827
   sca li PrZ1 
   sca PrZ0=1-0.2827
   sca li PrZ0
   
  
   }
   
   
   if `outcome'==currentViolent {
   global tsls_outcome "sviolent"
   } 
   else {
   global tsls_outcome "snonviolent"
   } 
   
   *Set the yeargroup to for the lindo estimates 
   global lindo_yeargroup 5
   
   di "$tsls_outcome"
   di "$race"
   di "$lindo_yeargroup"
   
   
   I2_pop_racial_be 
   mat b_`race'=e(b)
   
   /* The above program computes the bounds as in I2_pop_racial_be_0804 
   the below part picks up the elements in the white and nonwhite 
   results and the difference between the nonwhite lower bound and 
   the whites upper bound is computed when the loop finishes
   
   */ 

   mat LB_LNATE_nt_`race'=b_`race'[1,24]
   mat li LB_LNATE_nt_`race'
   
   mat UB_LNATE_nt_`race'=b_`race'[1,25]
   mat li UB_LNATE_nt_`race'
   
   restore 
	            
	       }
   }
	

  mat LNATE_diff=UB_LNATE_nt_1-LB_LNATE_nt_0
  mat colnames LNATE_diff = LNATE_diff 
  mat li LNATE_diff
        	  	  
mat I2=(b_1, b_0 , LNATE_diff)	    
mat li I2
		   
ereturn clear
ereturn post I2, esample (`touse') properties ("b")
ereturn local cmd "I2_pop_test_be"

end
